/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.ws;

import java.util.UUID;
import javax.inject.Inject;
import javax.jws.WebService;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.cdi.CdiCamelContext;
import org.apache.cxf.annotations.SchemaValidation;
import riv.service.catalogue._1.CareUnitCapacityType;
import riv.service.catalogue._1.ResultCodeEnum;
import riv.service.catalogue._1.ResultLevelEnum;
import riv.service.catalogue._1.ResultType;
import se.riv.service.catalogue.getcapacities.v1.rivtabp21.GetCapacitiesResponderInterface;
import se.riv.service.catalogue.getcapacitiesresponder.v1.GetCapacitiesResponseType;
import se.riv.service.catalogue.getcapacitiesresponder.v1.GetCapacitiesType;
import se.sll.service.catalogue.composite.helper.service.HsaCache;
import se.sll.service.catalogue.common.rest.response.HsaData;

/**
 *
 * @author khaleddaham
 */
@Slf4j
@WebService(
        endpointInterface = "se.riv.service.catalogue.getcapacities.v1.rivtabp21.GetCapacitiesResponderInterface",
        serviceName = "GetCapacities")
@SchemaValidation
public class GetCapacities implements GetCapacitiesResponderInterface {

    @Inject
    private CdiCamelContext camelCtx;
    @Inject
    private HsaCache hsaCache;

    @Override
    public GetCapacitiesResponseType getCapacities(String string, GetCapacitiesType request) {
        UUID logId = UUID.randomUUID();
        logId.toString();
        log.info("logId: {}, request: {}", logId.toString(), request.toString());
        GetCapacitiesResponseType response = new GetCapacitiesResponseType();
        response.setResult(new ResultType());
        try {
            ProducerTemplate producer = camelCtx.createProducerTemplate();
            if (request.getHSAId().isEmpty()) {
                response = (GetCapacitiesResponseType) producer.sendBody("direct:start:capacity:getall", ExchangePattern.InOut, null);
            } else {
                response = (GetCapacitiesResponseType) producer.sendBody("direct:start:capacity:byunitids", ExchangePattern.InOut, request.getHSAId());
            }
            //@TODO filter 
            log.debug("Found {} care units.", response.getOrganizationalUnit().size());
        } catch (CamelExecutionException ex) {
            log.error(ex.getMessage(), ex);
            response.getResult().setLevel(ResultLevelEnum.ERROR);
            response.getResult().setCode(ResultCodeEnum.APPLICATION_ERROR);
            response.getResult().setMessage(ex.getMessage());
        }
        response.getResult().setLogId(logId.toString());
        return enrichHsaData(response);
    }

    private GetCapacitiesResponseType enrichHsaData(GetCapacitiesResponseType response) {
        for (CareUnitCapacityType o : response.getOrganizationalUnit()) {
            HsaData hsa = hsaCache.getHsa(o.getHSAId());
            if (hsa == null) {
                continue;
            }
            try {
                o.setOrganizationName(hsa.getRelativeDistinguishedName());
            } catch (Exception ex) {
            }
        }
        return response;
    }
}